<?php
/**
调用接口时，商户号：26361，秘钥：WN}zNqp}
 */
$payment_lang = array(
    'name'	=>	'汇潮支付',
    'ecpss_account'	=>	'商户号',
    'ecpss_password'		=>	'密钥',
    'GO_TO_PAY'	=>	'前往汇潮支付在线支付',
    'VALID_ERROR'	=>	'支付验证失败',
    'PAY_FAILED'	=>	'支付失败',
);
$config = array(
    'ecpss_account'	=>	array(
        'INPUT_TYPE'	=>	'0',
    ), //商户号
    'ecpss_password'	=>	array(
        'INPUT_TYPE'	=>	'0'
    ), //密钥
);
/* 模块的基本信息 */
if (isset($read_modules) && $read_modules == true)
{
    $module['class_name']    = 'Ecpss';

    /* 名称 */
    $module['name']    = $payment_lang['name'];


    /* 支付方式：1：在线支付；0：线下支付 */
    $module['online_pay'] = '1';

    /* 配送 */
    $module['config'] = $config;

    $module['lang'] = $payment_lang;
    $module['reg_url'] = 'https://merchant.ecpss.com/newReg.jsp';
    return $module;
}

// 支付宝支付模型
require_once(SITE_PATH.'system/libs/payment.php');
class Ecpss_payment implements payment {

    private function write_log(){
        $file_path = SITE_PATH.'system/logs/';
        $filename = $file_path.'pay.log';
        if(!is_dir($file_path)){
            mkdir($file_path,0777,true);
        }
        $handle = fopen($filename,'a+');
        $content = "\r\n=============================START==============================\r\n";
        $content .= "PAY DATE:".date('Y-m-d H:i:s')."\r\n";
        $content .= "REQUEST_DATA:\r\n";
        $content .= var_export($_REQUEST,true);
        $content .= "\r\n";
        $content .= "==============================END==============================\r\n\r\n";
        fwrite($handle,$content,strlen($content));
        fclose($handle);
    }

    public function get_payment_code($payment_notice_id)
    {
        $payment_notice = M('payment_notice')->where(array('id'=>$payment_notice_id))->find();
        $money = round($payment_notice['money'],2);
        $payment_info = M('payment')->where(array('id'=>intval($payment_notice['payment_id'])))->field('id,config,logo')->find();
        $payment_info['config'] = unserialize($payment_info['config']);

        $subject = $payment_notice['deal_name']==""?"充值".format_price($payment_notice['money']):$payment_notice['deal_name'];

        $data_return_url = WEB_DOMAIN.'/payment/response/class_name/Ecpss';
        $data_notify_url = WEB_DOMAIN.'/payment/notify/class_name/Ecpss';

        $md5src = $payment_info['config']['ecpss_account']."&".$payment_notice['notice_sn']."&".$money."&".$data_return_url."&".$payment_info['config']['ecpss_password'];		//校验源字符串
        $orderTime =date('YmdHis',$payment_notice['create_time']);
        $defaultBankNumber ="";
        $SignInfo = strtoupper(md5($md5src));		//MD5检验结果
        $parameter = array(
            'MerNo'           => $payment_info['config']['ecpss_account'],
            'BillNo'    => $payment_notice['notice_sn'],
            'Amount'        => $money,
            'ReturnURL'        => $data_return_url,
            'AdviceURL'           => $data_notify_url,
            'orderTime'      => $orderTime,
            'defaultBankNumber'             => $defaultBankNumber,
            'SignInfo'          => $SignInfo,
            'Remark'      => '',
            'products'    => $subject
        );

        ksort($parameter);
        reset($parameter);

        $form = '';

        foreach ($parameter AS $key => $val)
        {
            $form .= sprintf('<input type="hidden" name="%s" value="%s">',$key,$val);
        }

        $payLinks = "<form action='https://pay.ecpss.com/sslpayment' id='jumplink' method='post'>{$form}正在连接支付接口...</form>";
        $payLinks.='<script type="text/javascript">document.getElementById("jumplink").submit();</script>';

        return $payLinks;
    }

    public function response($request)
    {
        $payment = M('payment')->where(array('class_name'=>'Ecpss'))->field('id,config')->find();
        $payment['config'] = unserialize($payment['config']);


        /* 检查数字签名是否正确 */
        ksort($request);
        reset($request);
        $md5_key = $payment['config']['ecpss_password'];
        $BillNo = $request["BillNo"];
        $Amount = $request["Amount"];
        $Succeed = $request["Succeed"];
        $Result = $request["Result"];
        $SignMD5info = $request["SignMD5info"];
        //备注
        $Remark = $request["Remark"];
        $md5src = $BillNo."&".$Amount."&".$Succeed."&".$md5_key;
        $sign = strtoupper(md5($md5src));

        $this->write_log();

        if($SignMD5info != 'FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF'){
            if ($sign != $SignMD5info)
            {
                exit("签名验证失败");
            }
        }



        if($Succeed=="88"){
            require_once SITE_PATH."system/libs/cart.php";
            $rs = payment_paid($BillNo,$BillNo);
            return array('status'=>1,'info'=>$rs['info'],'url'=>$rs['jump']);
        }else{
            return array('status'=>0,'info'=>$Result,'url'=>'/');
        }
    }

    public function notify($request)
    {
        return $this->response($request);
    }
    //获取支付图标
    public function get_display_code()
    {
        $payment_item = M('payment')->where(array('class_name'=>'Ecpss'))->find();
        if($payment_item)
        {
            $html = "<div style='float:left;'>".
                "<input type='radio' name='payment' checked value='".$payment_item['id']."' />&nbsp;".
                $payment_item['name'].
                "：</div>";
            if($payment_item['logo']!='')
            {
                $html .= "<div style='float:left; padding-left:10px;'><img src='{$payment_item['logo']}' /></div>";
            }
            $html .= "<div style='float:left; padding-left:10px;'>".nl2br($payment_item['description'])."</div>";
            return $html;
        }
        else
        {
            return '';
        }
    }

}
?>